package com.test.daily.leetcode.y2022.m04.day0421.v03;

/**
 * @author Tom on 2022/4/21
 */
public class Solution {
    public static void main(String[] args) {
        int[] arr = new int[]{3, 4, 5, 6, 9, 11, 23, 2, 11, 1};
//        selectSort(arr);
//        bubbleSort(arr);
        insertSort(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(" " + arr[i]);
        }
    }

    public static void insertSort(int[] arr){
        int N = arr.length;
        for (int i = 0; i < N; i++) {
            for (int j = i; j > 0 && arr[j]<arr[j-1]; j--) {
                swap(arr, j-1, j);
            }
        }
    }

    public static void bubbleSort(int[] arr) {
        int N = arr.length;
        for (int i = N - 1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                if (arr[j] > arr[j + 1]) {
                    swap(arr, j, j + 1);
                }
            }
        }
    }

    public static void selectSort(int[] arr) {
        int N = arr.length;
        for (int i = 0; i < N; i++) {
            int minIndex = i;
            for (int j = i + 1; j < N; j++) {
                minIndex = arr[j] < arr[minIndex] ? j : minIndex;
            }
            swap(arr, i, minIndex);
        }
    }

    public static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
}
